

#include <stdio.h>
#include <stdlib.h>

#include "m_matrix.h"
#include "berechnung.h"

matrix createMatrix(double low, double high, double stepsize)
{
    int i;
    int width;
    int height;
    MATRIX_CONTENT_TYPE  *zw;
    MATRIX_TYPE mat;
    matrix m = {NULL,0,0,0,0,0};
    
    /*Width & Height ausrechnen...*/
    width = ((high-low)/stepsize)+1;
    height = width;
    
    
    /*zw = (MATRIX_CONTENT_TYPE*) calloc(width*height, sizeof(MATRIX_CONTENT_TYPE));*/
    zw = (MATRIX_CONTENT_TYPE*) malloc(width*height*sizeof(MATRIX_CONTENT_TYPE));
    
    if(zw == NULL){
        printf("Err: Failed to allocate a block of memory, a NULL pointer is returned.  [1]\n");
        exit(1);
    }
    
    /*mat = calloc(width, sizeof(MATRIX_CONTENT_TYPE*));*/
    mat = malloc(width*sizeof(MATRIX_CONTENT_TYPE*));
    if(mat == NULL){
        printf("Err: Failed to allocate a block of memory, a NULL pointer is returned.  [2]\n");
        exit(1);
    }
    
    
    for(i = 0; i < width; ++i)
    {        
        mat[i] = &zw[i*width];
    }
            
    m.content = mat;
    m.stepsize = stepsize;
    m.start = low;
    m.end = high;
    m.width = width;
    m.height = height;    
    
    return m;
}

void clearMatrix(matrix m)
{
    int i, j;
    for(i = 0; i<m.width; ++i)
    {
        for(j = 0; j<m.height; ++j)
        {
            m.content[i][j] = 0;
        }
    }
}

void freeMatrix(matrix m)
{
    MATRIX_TYPE mt = m.content;
    free(*mt);
    free(mt);
    m.stepsize=0;
    m.start=0;
    m.end=0;
    m.width=0;
    m.height=0;
}

void printMatrix(matrix m)
{
    int i,j;
    printf("Matrix\t\t");
    for(i = 0; i < m.height; ++i)
    {
        printf("%f\t", m.start+(m.stepsize*i));
    }
    printf("\n");
    for( i = 0; i < m.width; ++i)
    {
        printf("%f>\t", m.start+(m.stepsize*i));
        for( j = 0; j < m.height; ++j)
        {
            printf(MATRIX_CONTENT_FORMAT, m.content[i][j]);
        }
        printf("\n");
    }
}


void setInMatrix(matrix m, double x, double y)
{
    double xPos, yPos;
    
    /*xPos = x-m.start*/
}

